import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:screen_capture/screen_capture.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _res = 'Unknow';
  final _screenCapturePlugin = ScreenCapture();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _screenCapturePlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  // preventScreenCapture
  Future<void> preventScreenCapture() async {
    String res;
    try {
      res =
          await _screenCapturePlugin.preventScreenCapture() ?? 'Unknown platform version';
    } on PlatformException {
      res = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _res = res;
    });
  }

  // allowScreenCapture
  Future<void> allowScreenCapture() async {
    String res;
    try {
      res =
          await _screenCapturePlugin.allowScreenCapture() ?? 'Unknown platform version';
    } on PlatformException {
      res = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _res = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('Running on: $_platformVersion\n'),

              // www
              ElevatedButton(
                onPressed: preventScreenCapture,
                child: Text('防止屏幕截屏'),
              ),
              ElevatedButton(
                onPressed: allowScreenCapture,
                child: Text('允许屏幕截屏'),
              ),
              Text('调用ArkTS方法 ==> $_res'),
              
            ],
          ),
        ),
      ),
    );
  }
}
